using System;
using System.IO;
using FluentMigrator.Runner;
using Microsoft.Extensions.DependencyInjection;

namespace WebForm.Migrator
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            var serviceProvider = CreateServices();
            using (var scope = serviceProvider.CreateScope())
            {
                UpdateDatabase(scope.ServiceProvider);
            }
        }

        private static IServiceProvider CreateServices()
        {
	        // 定位到WebForm项目的App_Data/database.db
	        var dbPath = Path.GetFullPath(Path.Combine(
		        AppDomain.CurrentDomain.BaseDirectory, 
		        @"..\..\..\WebForm\App_Data\database.db"
	        ));
	        var connectionString = $"Data Source={dbPath};Version=3;";
            return new ServiceCollection()
                .AddFluentMigratorCore()
                .ConfigureRunner(rb => rb
                    .AddSQLite()
                    .WithGlobalConnectionString(connectionString)
                    .ScanIn(typeof(Program).Assembly).For.Migrations())
                .AddLogging(lb => lb
                    .AddFluentMigratorConsole())
                .BuildServiceProvider(false);
        }
        private static void UpdateDatabase(IServiceProvider serviceProvider)
        {
            var runner = serviceProvider.GetRequiredService<IMigrationRunner>();
            runner.MigrateUp();
        }
    }
}
